/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于线下支付的一些业务逻辑操作
 * @author WangXinLei
 * @date 2014-11-18
 */
package com.chnbst.bstyun.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Deal;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.CashflowVo;
import com.chnbst.bstyun.model.vo.DealVo;
import com.chnbst.bstyun.model.vo.UserVo;
import com.chnbst.bstyun.service.CashflowService;
import com.chnbst.bstyun.service.DealService;
import com.chnbst.bstyun.service.UserService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author 
 *
 */
@Controller
@RequestMapping("/sysAdmin/accountMoney")
public class AccountMoneyAdminController {
	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;
	@Autowired
	private UserService userService;
	@Autowired
	private CashflowService cashflowService;
	@Autowired
	private DealService dealService;
	
	/**
	 * @return 账户余额列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/accountMoneyList",method=RequestMethod.GET)
	public ModelAndView accountMoneyList()throws Exception{
		return new ModelAndView("admin/financeManager/accountMoneyManager");
	}
	
	/**
	 * @return 线下支付列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/linePaymentList",method=RequestMethod.GET)
	public ModelAndView linePaymentList()throws Exception{
		return new ModelAndView("admin/financeManager/linePaymentManager");
	}
	
	/**
	 * @return 第三方支付列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/payByOthersList",method=RequestMethod.GET)
	public ModelAndView payByOthersList()throws Exception{
		return new ModelAndView("admin/financeManager/payByOthersManager");
	}
	
	/**
	 * Ajax 账户余额 主界面
	 * @return 用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/accountMoneyList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> accountMoneyList(String page,String last,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		String accountMoney = request.getParameter("accountMoney");
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		
		map.put("accountMoney", accountMoney);
		
		int totalRows = userService.selectUserCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "user_id");
		map.put("sortOrder", "desc");
			
		//计算账户总额
		Double totalAccount = 0.00;
		Double totalAccountMoney = userService.selectAccountMoney(map);
		if(totalAccountMoney != null){
			totalAccount = totalAccountMoney;
		}   
		
		//计算第三方支付的总金额
		Double recoveryAccount = 0.00;
		Double alipayAccount = cashflowService.selectAccountMoneyByPayWayId(1);
		Double chinapayAccount = cashflowService.selectAccountMoneyByPayWayId(3);
		if(alipayAccount != null && chinapayAccount != null) {
			recoveryAccount = alipayAccount + chinapayAccount;
		}else if(alipayAccount == null && chinapayAccount == null){
			recoveryAccount = 0.00;
		}else{
			if(alipayAccount == null ){
				recoveryAccount = chinapayAccount; 
			}else{
				recoveryAccount = alipayAccount; 
			}
		}
		
		//计算线下支付的总金额
		Double lineAccount = 0.00;
		Double paymentAccount = cashflowService.selectAccountMoneyByPayWayId(4);
		if(paymentAccount != null){
			lineAccount = paymentAccount;
		}
		
		List<UserVo> userList = userService.selectUserVoList(map);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("userList", userList);
		resultMap.put("recoveryAccount", recoveryAccount);
		resultMap.put("lineAccount", lineAccount);
		resultMap.put("totalAccount", totalAccount);
		
		return resultMap;
	}

	/**
	 * Ajax 线下支付 主界面
	 * @return 现金收支明细列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/linePaymentList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> linePaymentList(String page,String last)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		map.put("payWayId",4);
		int totalRows = cashflowService.selectCashflowsCountByPayWayId(map);
		
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "cashflow_id");
		map.put("sortOrder", "desc");
		//map.put("payWayId", 1);
		
		//计算线下支付的总金额
		Double lineAccount = 0.00;
		Double paymentAccount = cashflowService.selectAccountMoneyByPayWayId(4);
		if(paymentAccount != null){
			lineAccount = paymentAccount;
		}
		
		List<CashflowVo> userList = cashflowService.selectCashflowsByPayWayId(map);
		
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("userList", userList);	
		resultMap.put("lineAccount", lineAccount);
		
		return resultMap;
	}
	
	/**
	 * Ajax 第三方支付 主界面
	 * @return 现金收支明细列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/payByOthersList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> payByOthersList(String page,String last,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String payWayId = request.getParameter("payWayId");
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		if(payWayId == null||payWayId == ""){
			//银联支付和支付宝支付，payWayId设置为2
			map.put("payWayId", 2);
		}else{
			map.put("payWayId", payWayId);
		}
		
		int totalRows = cashflowService.selectCashflowsCountByPayWayId(map);
		
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "cashflow_id");
		map.put("sortOrder", "desc");
		//map.put("payWayId", 0);
		
		//计算第三方支付的总金额
		Double recoveryAccount = 0.00;
		Double alipayAccount = cashflowService.selectAccountMoneyByPayWayId(1);
		Double chinapayAccount = cashflowService.selectAccountMoneyByPayWayId(3);
		if(alipayAccount != null && chinapayAccount != null) {
			recoveryAccount = alipayAccount + chinapayAccount;
		}else if(alipayAccount == null && chinapayAccount == null){
			recoveryAccount = 0.00;
		}else{
			if(alipayAccount == null ){
				recoveryAccount = chinapayAccount; 
			}else{
				recoveryAccount = alipayAccount; 
			}
		}
		
		List<CashflowVo> userList = cashflowService.selectCashflowsByPayWayId(map);
		
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("userList", userList);	
		resultMap.put("recoveryAccount", recoveryAccount);
		
		return resultMap;
	}
	
	/**
	 * 查询用户交易清单列表
	 * @param userId
	 * @return 用户交易清单列表
	 * @throws Exception
	 */
	@RequestMapping(value="/userDealList/{userId}",method=RequestMethod.GET)
	public ModelAndView userDealList(@PathVariable Integer userId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		User user = userService.selectUserVoByUserId(userId);
		mav.addObject("user", user);
		mav.setViewName("admin/financeManager/accountMoneyDealList");
		
		return mav;
	}
	/**
	 * 根据ID查询单个用户数据的交易记录
	 * @return 交易记录列表
	 * @throws Exception
	 */
	@RequestMapping(value="/userDealList",method=RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object>  userDealList(Integer userId,String page,String last)throws Exception{
		
		User user = userService.selectUserInfoByUserId(userId);
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		
		int totalRows = dealService.selectDealCountByUserId(userId);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField","deal_id");
		map.put("sortOrder", "desc");
		
		map.put("userId",userId);
		map.put("isYun", user.getIsYun());
		
		List<DealVo> dealList = dealService.selectDealList(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("dealList", dealList);
		
		return resultMap;
	}
	
	/**
	 * 根据ID查询单条交易的详细信息
	 * @return 交易详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/userDealDetail/{dealId}",method=RequestMethod.GET)
	public ModelAndView userDealDetail(@PathVariable Integer dealId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		Deal userInfo = dealService.selectDealByDealId(dealId);
		
		//设置使用着的用户名
		User user = userService.selectUserVoByUserId(userInfo.getUserId());
		
		String userAccountName = user.getUserEmail();
		if(SignUtil.isEmpty(user.getUserEmail())){
			userAccountName = user.getUserTel();
		}
		
		//获取购买者的用户名
		UserVo userVoInfo = userService.selectUserVoByUserId(userInfo.getParentId());
		String parentName = userVoInfo.getUserEmail();
		if(SignUtil.isEmpty(userVoInfo.getUserEmail())){
			parentName = userVoInfo.getUserTel();
		}
		
		mav.addObject("userInfo", userInfo);
		mav.addObject("userAccountName", userAccountName);
		mav.addObject("parentName", parentName);
		mav.setViewName("admin/financeManager/accountMoneyDetail");
		
		return mav;
	}
	
	/**
	 * 根据ID查询单条线下支付现金收支明细的详细信息
	 * @return 线下支付现金收支详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/linePaymentDetail/{cashflowNum}",method=RequestMethod.GET)
	public ModelAndView linePaymentDetail(@PathVariable String cashflowNum)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		CashflowVo userInfo = cashflowService.selectCashflowByCashflowNum(cashflowNum);
		
		mav.addObject("userInfo", userInfo);
		mav.setViewName("admin/financeManager/linePaymentDetail");
		
		return mav;
	}
	
	/**
	 * 根据ID查询单条第三方支付现金收支明细的详细信息
	 * @return 第三方支付现金收支详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/payByOthersDetail/{cashflowNum}",method=RequestMethod.GET)
	public ModelAndView payByOthersDetail(@PathVariable String cashflowNum)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		CashflowVo userInfo = cashflowService.selectCashflowByCashflowNum(cashflowNum);
		
		mav.addObject("userInfo", userInfo);
		mav.setViewName("admin/financeManager/payByOthersDetail");
		
		return mav;
	}
}
